package com.leo.chailv.controller;

import com.leo.chailv.entity.Message;
import com.leo.chailv.entity.Travel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class MainController {

    @RequestMapping("/")
    public String showIndex(){
        return "index";
    }

    /**
     * 添加差旅信息
     * @param travel
     * @return
     */
    @RequestMapping("/addTravel")
    public @ResponseBody Message addTravel(Travel travel, HttpSession httpSession){
        //travel封装的车票等信息
        System.out.println(travel);
        //插入数据库，并将id返回
        httpSession.setAttribute("travelEntity",travel);
        //将返回的id取到传入message
        return new Message(true,"添加成功!",1);
    }

    /**
     * 显示详情页
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/details/{id}")
    public String showDetails(@PathVariable("id") int id, Model model,HttpSession httpSession){
        //拿到id，在数据库查对应信息，然后返回查到的travel，这里travel先写死
        model.addAttribute("travel",httpSession.getAttribute("travelEntity"));
        return "details";
    }
}
